package futian
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.filesystem.File;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	import flash.utils.clearTimeout;


	public class CameraPage extends Sprite
	{
		private static var _main:CameraPage = null;
		public static function get main():CameraPage
		{
			if(_main==null)
				_main = new CameraPage();
			return _main;
		}

		public static var photoBmp:Bitmap;

		public function CameraPage()
		{
			_main = this;
			addChild(new Bitmap(test.main.bg.bitmapData));

			width = test.stageW;
			height = test.stageH;

			photoBtn = new BtnBase(countDown,test.newBtn("拍照"));
			//photoBtn = new BtnBase(countDown,new Assets.takephoto_png);
			signBtn = new BtnBase(toSign,test.newBtn("签名上传"));
			//signBtn = new BtnBase(toSign,new Assets.sign_png);
			uploadBtn = new BtnBase(toUpload,test.newBtn("直接上传"));
			//uploadBtn = new BtnBase(toUpload,new Assets.upload_png);
			backBtn = new BtnBase(back,test.newBtn("主页"));
			//backBtn = new BtnBase(back,new Assets.back_png);
			resetBtn = new BtnBase(resetCamera,test.newBtn("重新拍摄"));
			//resetBtn = new BtnBase(resetCamera,new Assets.rephoto_png);
			//resetBtn.width = backBtn.width = photoBtn.width = signBtn.width = uploadBtn.width = 300;
			//resetBtn.height = backBtn.height = photoBtn.height = signBtn.height = uploadBtn.height = 100;

			addChild(photoBtn);
			addChild(signBtn);
			addChild(uploadBtn);
			addChild(backBtn);
			addChild(resetBtn);

			backBtn.x = 1548;
			backBtn.y = 910; 
			photoBtn.x = 1494;
			photoBtn.y = 158;
			resetBtn.x = signBtn.x = uploadBtn.x = 1531;
			resetBtn.y = 440;
			uploadBtn.y = 159;
			signBtn.y = 300;


			width = test.stageW;
			height = test.stageH;

			clearTimeout(timeoutId);
			timeoutId = setTimeout(initCamera,100);
		}

		private function initCamera():void
		{
			clearTimeout(timeoutId);

			if(photoContainer==null)
			{
				photoContainer = new Sprite();
				with(photoContainer.graphics)
				{
					beginFill(0x0);
					drawRect(0,0,1230,700);
					endFill();
				}
				addChild(photoContainer);
				photoContainer.x = 158;
				photoContainer.y = 154;
				photoContainer.addChild(MyCam.main);

				cameraW = photoContainer.width;
				cameraH = photoContainer.height;
				MyCam.main.start_photo();
			}
		}

		private var frameBmp:Bitmap;
		private var photoContainer:Sprite;
		private var listH:int = 0;
		private var arrloader:ArrLoader;
		private function allLoad(e:Event):void
		{
			logs.adds("all loaded");

			addChild(backBtn);
			addChild(photoBtn);
			addChild(resetBtn);
			addChild(signBtn);
			addChild(uploadBtn);
		}


		public static var cameraW:int = 0;
		public static var cameraH:int = 0;


		private function resetCamera():void
		{
			resetBtn.visible = signBtn.visible = uploadBtn.visible = false;
			mylist.visible = photoBtn.visible = true;

			if(timeTxt){
				timeTxt.visible = false;
			}
			if(photoBmp)
			{
				if(photoBmp.parent)
					photoBmp.parent.removeChild(photoBmp);
				photoBmp.bitmapData.dispose();
				photoBmp = null;
			}
		}

		private static var timeoutId:uint;
		private static var timeCounter:int;
		private static var timeTxt:TextField;
		private function countDown():void
		{
			resetBtn.visible = signBtn.visible = uploadBtn.visible = false;
			mylist.visible = photoBtn.visible = false;

			if(timeTxt){
				timeTxt.visible = false;
			}
			if(photoBmp)
			{
				if(photoBmp.parent)
					photoBmp.parent.removeChild(photoBmp);
				photoBmp.bitmapData.dispose();
				photoBmp = null;
			}


			timeCounter = 5;
			CONFIG::debugging{
				timeCounter = 1;
			}
			showTimeRemain();
		}

		private function showTimeRemain():void
		{
			clearTimeout(timeoutId);
			if(timeTxt==null)
			{
				timeTxt = ViewSet.makeTxtLine("",100,0xffffff);
				timeTxt.alpha=.3;
			}
			timeTxt.text = ""+timeCounter;
			timeTxt.visible = true;
			addChild(timeTxt);
			ViewSet.fullCenter(timeTxt,photoContainer.x,photoContainer.y,photoContainer.width,photoContainer.height);

			if(timeCounter==0)
			{
				timeTxt.visible = false;
				resetBtn.visible = signBtn.visible = uploadBtn.visible = true;
				mylist.visible = photoBtn.visible = false;

				photoBmp = new Bitmap(WriteData.displayObject_bitmapDate(photoContainer));
				addChild(photoBmp);
				photoBmp.x = photoContainer.x;
				photoBmp.y = photoContainer.y;
			}else{
				timeCounter--;
				timeoutId = setTimeout(showTimeRemain,1000);
			}
		}


		private var mylist:MyList = new MyList;

		private var photoBtn:BtnBase;
		private var backBtn:BtnBase;
		private var resetBtn:BtnBase;
		private var signBtn:BtnBase;
		private var uploadBtn:BtnBase;

		private function toSign():void
		{
			logs.adds("签名");
			SignPage.show();
		}
		private function toUpload():void
		{
			BgSelect.show();
		}

		private static var indexTxt:TextField;
		public static function show():void
		{
			main.visible = true;
			test.main.addChild(main);
			main.resetCamera();

			indexTxt = Datas.setIndexTxt(indexTxt);
			main.addChild(indexTxt);
		}
		public static function back():void
		{
			clearTimeout(timeoutId);
			if(timeTxt){
				timeTxt.visible = false;
			}
			main.visible = false;
			FirstPage.show();
		}
	}
}
